package org.example.test_drools;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class MergeUtil {

    public static <T> T[] mergeMap(T[] a, T[] b) {
        if (a == null) {
            if (b == null) {
                return (T[]) new Order[0];
            } else {
                return b;
            }
        } else {
            if (b == null || b.length < 1) {
                return a;
            }
        }
        T[] c = (T[]) new Object[a.length + b.length];
        for (int i = 0; i < a.length; i++) {
            c[i] = a[i];
        }
        for (int j = 0; j < b.length; j++) {
            c[j + a.length] = b[j];
        }
        return c;
    }

    public static <T> T[] flatMergeMap(T[] a, T[] b) {
        T[] c = mergeMap(a, b);
        if (c == null || c.length < 1) {
            return c;
        }
        LinkedHashMap<T, T> map = new LinkedHashMap<T, T>();
        for (T t : c) {
            map.put(t, t);
        }
        List<T> list = new ArrayList<T>(map.values());
        return (T[]) list.toArray();
    }

}
